<template>
  <div class="mod-category">
    <el-form :inline="true" size="small" :model="dataForm">
      <el-form-item>
        <el-button @click="addOrUpdateHandle()">新增一级分类</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      highlight-current-row
      size="mini"
      row-key="id"
      border
      lazy
      :load="load"
      ref="dataListTable"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="index" align="center" label="编号" width="100" />
      <el-table-column
        prop="name"
        header-align="center"
        min-width="150"
        align="left"
        label="分类名称"
      ></el-table-column>

      <el-table-column
        prop="icon"
        header-align="center"
        align="center"
        label="图标"
      ></el-table-column>

      <el-table-column
        prop="dataState"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === -1" size="small" type="danger"
            >禁用</el-tag
          >
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序"
      ></el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      ></el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
             v-if="scope.row.level !== 3"
            @click="addOrUpdateHandle(scope.row, 1)"
            >添加子分类</el-button
          >

           <el-dropdown  v-if="scope.row.level === 3" @command="handleCommand(scope.row, $event)">
            <el-button size="mini">
              绑定 <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">

              <el-dropdown-item command="updateBrand"
                >绑定品牌</el-dropdown-item
              >
              <el-dropdown-item divided command="updateBaseAttr"
                >绑定规格参数</el-dropdown-item
              >
              <el-dropdown-item command="updateSaleAttr"
                >绑定销售参数</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>

          <el-dropdown @command="handleCommand(scope.row, $event)">
            <el-button size="mini">
              操作 <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="addOrUpdateHandle"
                >修改</el-dropdown-item
              >
              <el-dropdown-item command="deleteHandle">删除</el-dropdown-item>

              <el-dropdown-item
                v-if="scope.row.dataState === -1"
                command="enableHandle"
                >启用</el-dropdown-item
              >
              <el-dropdown-item v-else command="disableHandle"
                >冻结</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <add-or-update-brand
      v-if="addOrUpdateBrandVisible"
      ref="addOrUpdateBrand"
      @refreshDataList="getDataList"
    ></add-or-update-brand>

    <add-or-update-base-attr
      v-if="addOrUpdateBaseAttrVisible"
      ref="addOrUpdateBaseAttr"
      @refreshDataList="getDataList"
    ></add-or-update-base-attr>

    <add-or-update-sale-attr
      v-if="addOrUpdateSaleAttrVisible"
      ref="addOrUpdateSaleAttr"
      @refreshDataList="getDataList"
    ></add-or-update-sale-attr>

  </div>
</template>


<script>
import AddOrUpdate from "./category-add-or-update";
import AddOrUpdateBrand from "./category-add-or-update-brand";
import AddOrUpdateBaseAttr from "./category-add-or-update-base-attr";
import AddOrUpdateSaleAttr from "./category-add-or-update-sale-attr";
import {
  productCategoryPageList,
  removeProductCategory,
  enableProductCategory,
  disableProductCategory,
} from "@/api/product";
import { treeDataTranslate } from "@/utils";

export default {
  data() {
    return {
      dataForm: {
        name: "",
      },
      dataList: [],
      dataListLoading: false,
      addOrUpdateSaleAttrVisible: false,
      addOrUpdateBaseAttrVisible: false,
      addOrUpdateBrandVisible: false,
      addOrUpdateVisible: false,
      dataListSelections: [],
    };
  },
  components: {
    AddOrUpdate,AddOrUpdateBrand,AddOrUpdateBaseAttr,AddOrUpdateSaleAttr
  },
  activated() {
    this.getDataList();
  },
  methods: {
    query() {
      this.getDataList();
    },
    handleCommand(row, command) {
      if (command == "enableHandle") {
        this.enableHandle(row);
      } else if (command == "disableHandle") {
        this.disableHandle(row);
      } else if (command == "deleteHandle") {
        this.deleteHandle(row.id);
      } else if (command == "addOrUpdateHandle") {
        this.addOrUpdateHandle(row, 2);
      } else if (command == "updateBrand") {
        this.addOrUpdateBrand(row);
      } else if (command == "updateBaseAttr") {
        this.addOrUpdateBaseAttr(row);
      } else if (command == "updateSaleAttr") {
        this.addOrUpdateSaleAttr(row);
      }
    },
    load(tree, treeNode, resolve) {
      this.loadTablelist(
        {
          id: tree.id,
        },
        resolve
      );
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      productCategoryPageList({ id: 0 }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = treeDataTranslate(res.data, "id");
        }
        this.dataListLoading = false;
      });
    },

    loadTablelist(data, resolve) {
      this.dataListLoading = true;
      productCategoryPageList(data).then((res) => {
        if (res && res.code === 0) {
          resolve(treeDataTranslate(res.data, "id"));
        }
        this.dataListLoading = false;
      });
    },

    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data, addChildren) {
      this.addOrUpdateVisible = true;
      if (data) {
        data.addChildren = addChildren;
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
     // 新增 / 修改
    addOrUpdateBrand(data) {
      this.addOrUpdateBrandVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdateBrand.init(data);
      });
    },
     addOrUpdateBaseAttr(data) {
      this.addOrUpdateBaseAttrVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdateBaseAttr.init(data);
      });
    },
     addOrUpdateSaleAttr(data) {
      this.addOrUpdateSaleAttrVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdateSaleAttr.init(data);
      });
    },
    enableHandle(row) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableProductCategory({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(row) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableProductCategory({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定是否进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          removeProductCategory({ ids: ids }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>